import json
from time import sleep

from base.base import Base
from dao.mysqlhelper import MySQLHelper

# 组装sql

mysql=MySQLHelper()
# SQL 查询语句
sql = """
SELECT
	mc.catalog_id,
	ma.model_vid 
FROM
	t_fusion_model_catalog mc
	LEFT JOIN t_fusion_model fm ON mc.catalog_id = fm.catalog_id
	LEFT JOIN t_fusion_model_attribute ma ON fm.model_vid = ma.model_vid 
WHERE
	( mc.namepath LIKE '空间资源%' OR mc.namepath LIKE '感知资源%' OR mc.namepath LIKE '服务资源%' ) 
	AND mc.del_flag = 0
	AND fm.del_flag = 0
	AND ma.del_flag = 0 
	AND mc.node_level != 1 
GROUP BY
	ma.model_vid 
HAVING
	count(*)>0
"""
re=mysql.select(sql)
# print(re)
# print(len(re))

# 新接口直接执行
base=Base()
param_type='application/json'
headers={
    'appcode':'FUSION',
    'authorization':'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiIxODM4NTE3NDc2ODU5MTcwODE3Iiwic3ViIjoienpoIiwiYXVkIjoidGVuYW50IiwidW5hbWUiOiJ6emgiLCJhcHBJZCI6Ijk2OWYyYjBjMzY2YTQ2NWE5ZmU4ZGZmMDEyZDE3MjFhIiwiaXNzIjoiaWRhYXMiLCJ0ZW5hbnRJZCI6ImRlZmF1bHRUZW5hbnRJZCIsInV0eXBlIjoidXNlciIsInJuZCI6ImJwbWZnMGVuIiwiZXhwIjoxNzM1MzU0MzMzLCJpYXQiOjE3MzUyNjc5MzN9.BOa3YaF2SAn3-BlJBblQqwRAdAnZW2MZSDUYj647h8M'
}
for catalog in re:
    data={
        "attMappingList": [
            {
                "dAttName": "",
                "dattName": "",
                "sattName": ""
            }
        ],
        "categoryId": catalog[0],
        "sql": ""
    }
    url='http://192.168.143.99:10092/idaas/idaas-gateway/talkweb-fusion-server/resource/insertResourceFromEntity'
    print(data)
    # res=json.loads(base.send_post(url,param_type,headers=headers,data=data))['msg']
    # print(f"{catalog[0]} 结果为：{res}")
    sleep(10)

















# 原来的接口，拼装sql
# model_list=[]
# for model_vid in re:
#     # SQL 查询每个分类id对应的属性
#     sql = f"""
#     select attribute_code,attribute_name from tw_gdcp.t_fusion_model_attribute where model_vid={model_vid[1]} and del_flag=0
#     """
#     res=mysql.select(sql)
#     list=[model_vid[0],res]
#     model_list.append(list)
# # print(model_list)
#
# def extend_attributes(model_list):
#     sql_list=[]
#     str1="SELECT e.extend_attributes,e.unified_code AS ALL_TYM,e.altitude,e.biz_code,e.geosot_code,e.belong_unit,e.manage_unit,e.category_id,e.latitude,e.longitude,e.entity_name AS ALL_ADRR_NAME,concat( e.std_address, e.detail_address ) AS ALL_ADDRESS,( SELECT unified_code FROM tw_gdcp.entity_info WHERE entity_id = e.belonging_space AND del_flag = 0 ) AS GSKJ_TYM,( SELECT entity_name FROM tw_gdcp.entity_info WHERE entity_id = e.belonging_space AND del_flag = 0 ) AS GSKJ_NAME,"
#     for model in model_list:
#         str2=f" FROM tw_gdcp.entity_info e WHERE e.category_id = {model[0]} AND e.del_flag = 0"
#         for attrs in model[1]:
#             if attrs[0] not in ['latitude','altitude','ALL_TYM','longitude','GSKJ_NAME','ALL_ADDRESS','biz_code','geosot_code','belong_unit','manage_unit','GSKJ_TYM','category_id','ALL_ADRR_NAME']:
#                 str1=str1 +f"JSON_UNQUOTE(JSON_EXTRACT( e.extend_attributes, '$.{attrs[0]}' )) AS {attrs[0]},"
#         str3=str1.rstrip(",")+str2
#         sql_list.append(str3)
#     return sql_list
#
# sql_list=extend_attributes(model_list)
# print(sql_list)
#
# base=Base()
# param_type='application/json'
# headers={
#     'appcode':'FUSION',
#     'authorization':'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiIxODM4NTE3NDc2ODU5MTcwODE3Iiwic3ViIjoienpoIiwiYXVkIjoidGVuYW50IiwidW5hbWUiOiJ6emgiLCJhcHBJZCI6Ijk2OWYyYjBjMzY2YTQ2NWE5ZmU4ZGZmMDEyZDE3MjFhIiwiaXNzIjoiaWRhYXMiLCJ0ZW5hbnRJZCI6ImRlZmF1bHRUZW5hbnRJZCIsInV0eXBlIjoidXNlciIsInJuZCI6ImJwbWZnMGVuIiwiZXhwIjoxNzM1MzU0MzMzLCJpYXQiOjE3MzUyNjc5MzN9.BOa3YaF2SAn3-BlJBblQqwRAdAnZW2MZSDUYj647h8M'
# }
# # if sql_list !=[]:
# sleep(2)
# for sql in sql_list:
#     data={
#         "attMappingList": None,
#         "categoryId": model_vid[0],
#         "sql": sql
#     }
#     url='http://192.168.143.99:10092/idaas/idaas-gateway/talkweb-fusion-server/resource/batchInsertResource'
#     print(sql)
#     sleep(2)
#     print(data)
#     res=json.loads(base.send_post(url,param_type,headers=headers,data=data))['msg']
#     print(f"{model_vid[0][0]} 结果为：{res}")
